//
//  ATVStringPropertyViewController.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 12/1/11.
//  Copyright 2011 Molamil. All rights reserved.
//

#import "ATVStringPropertyViewController.h"
#import "ATVInstanceStringProperty.h"

@implementation ATVStringPropertyViewController

@synthesize titleLabel;
@synthesize currentTextView;
@synthesize midiButton;
@synthesize copyValueButton;
@synthesize resetValueButton;
@synthesize clearValueButton;
@synthesize submitValueButton;
@synthesize orgValue;
@synthesize isMethod;
@synthesize methodButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateValueHandler:) name:@"updateValue" object:nil];
    
    return self;
}

- (void) updateValueHandler: (NSNotification*) note {
    NSDictionary* dict = (NSDictionary*)note.object;
    NSString* path = [dict valueForKey:@"path"];
    if([path isEqualToString:self.path]) {
        orgValue = [dict valueForKey:@"value"];
        [currentTextView setString:orgValue];
        [currentTextView setAcceptsTouchEvents:NO];
    }
}

- (void) destroy {
}

- (void) drawFromModel:(ATVInstanceStringProperty*) model {
    [titleLabel setStringValue: model.name];
    isMethod = model.isMethod;
}


- (IBAction)midiButtonMessage:(id)sender {}
- (IBAction)copyButtonMessage:(id)sender {
    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    [pasteboard clearContents];
    NSArray *objectsToCopy = [[NSArray alloc] initWithObjects:[currentTextView string], nil];
    [pasteboard writeObjects:objectsToCopy];
}
- (IBAction)clearButtonMessage:(id)sender {
    [currentTextView setString:@""];
}

- (IBAction)resetButtonMessage:(id)sender {
    [currentTextView setString:orgValue];
}
- (IBAction)submitButtonMessage:(id)sender {
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[currentTextView string], @"value", self.path, @"path", nil];

    if(!isMethod){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setSimpleValue" object:dict];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"executeMethod" object:dict];                
    }
}

@end